package com.hedl.practice.diyJava.multithreading.reentrantLock.read_write_lock.test;

import java.util.Arrays;
import java.util.concurrent.locks.*;

public class Study_1 {
    //private final Lock lock = new ReentrantLock();
    //private final Condition condition = lock.newCondition();    //添加线程唤醒和休眠功能
    private final ReadWriteLock rwlock = new ReentrantReadWriteLock();
    private final Lock readLock = rwlock.readLock();
    //private final Condition condition1 = readLock.newCondition();       //这样也可以
    private final Lock writeLock = rwlock.writeLock();
    private int[] counts = new int[10];

    public void inc(int index){
        writeLock.lock();
        try{
            counts[index] += 1;
        }finally {
            writeLock.unlock();
        }
    }

    public int[] get(){
        readLock.lock();
        try{
            return Arrays.copyOf(counts,counts.length);
        }finally {
            readLock.unlock();
        }
    }
}
